<?php declare(strict_types=1);

/**
 * @author      guosir
 * @copyright   2023-2025 owner
 **/

namespace Srv\Apps\Apis\Develop\Ctl;

use Srv\Apps\Apis\Common\Ctl\StorageCtl;
use Srv\Apps\Apis\Common\DefCtl\DefStorageCategoryCtl;
use Srv\Apps\Proto\DTO\Develop\AdOpenAddDTO;
use Srv\Apps\Proto\DTO\Develop\AdOpenEditDTO;
use Srv\Apps\Proto\DTO\Develop\AdOpenListDTO;
use Srv\Apps\Proto\Enum\Common\OpenAdMediaTypeEnum;
use Srv\Apps\Proto\Enum\Common\OpenAdShowModeEnum;
use Srv\Apps\Proto\Enum\YesOrNoEnum;
use Srv\Apps\Proto\VO\Develop\AdOpenListVO;
use Srv\Apps\Proto\VO\Develop\AdOpenVO;
use Srv\Libs\Common\CommArray;
use Srv\Libs\Common\CommTime;
use Srv\Libs\Frame\AuthData;
use Srv\Libs\Frame\ControllerAbstract;
use Srv\Libs\Frame\Page;
use Srv\Libs\Storage\DataMysql;

final class AdOpenCtl extends ControllerAbstract
{
    private ?DataMysql $DbAuto              = null;     // DB-Write

    /**
     * @return static
     * tag
     */
    final public static function tag():self
    {
        $thisObject                         = self::tagGet();
        if($thisObject instanceof self){
            $thisObject->DbAuto             = self::getDbAuto();
        }
        return $thisObject;
    }

    /**
     * @param int $adopId
     * @return array
     * getAdOpenSrc
     */
    public function getAdOpenSrc(int $adopId):array
    {
        if($adopId < 1) return [];
        $isFail         = false;
        $adOpenRecord   = $this->DbAuto->getDataOne('SELECT adop_id, ddlt_id, adop_title, adop_type, adop_media_one, adop_media_two, adop_media_thr, adop_bg_color, adop_show_mode, adop_show_num, adop_url, adop_show_time, adop_skip_time, adop_day_time_list, adop_status, adop_sort, adop_create_time, adop_update_time FROM '.$this->DbAuto->getTableNameAll('ad_open').' WHERE adop_id='.$adopId.' AND adop_delete=0', $isFail);
        if($isFail || !isset($adOpenRecord['adop_id'])) return [];
        return $adOpenRecord;
    }

    /**
     * @param Page $Page
     * @param array $whereData
     * @return AdOpenListVO|null
     * getAdOpenList
     */
    public function getAdOpenList(Page $Page, array $whereData): ?AdOpenListVO
    {
        $limitString        = $Page->getLimitString();
        $whereString        = ' WHERE adop_delete=0 ';
        // 检索条件
        if(count($whereData) > 0)foreach ($whereData as $key => $val){
            switch ($key){
                case AdOpenListDTO::_adopId:
                    $whereString    .= (strlen($whereString)>0?' AND ':'').'adop_id='. intval($val);
                    break;
                case AdOpenListDTO::_title:
                    $whereString    .= (strlen($whereString)>0?' AND ':'').'adop_title LIKE \'%'.$this->DbAuto->getRealEscapeString($val).'%\'';
                    break;
                case AdOpenListDTO::_type:
                    $whereString    .= (strlen($whereString)>0?' AND ':'').'adop_type='. intval($val);
                    break;
                case AdOpenListDTO::_showMode:
                    $whereString    .= (strlen($whereString)>0?' AND ':'').'adop_show_mode='. intval($val);
                    break;
                case AdOpenListDTO::_status:
                    $whereString    .= (strlen($whereString)>0?' AND ':'').'adop_status='. intval($val);
                    break;
            }
        }
        $isFail             = false;
        $sql                = 'SELECT SQL_CALC_FOUND_ROWS adop_id, ddlt_id, adop_title, adop_type, adop_media_one, adop_media_two, adop_media_thr, adop_bg_color, adop_show_mode, adop_show_num, adop_url, adop_show_time, adop_skip_time, adop_day_time_list, adop_status, adop_sort, adop_create_time, adop_update_time FROM '.$this->DbAuto->getTableNameAll('ad_open').$whereString.' ORDER BY adop_id DESC '.$limitString;
        $adOpenRecordList   = $this->DbAuto->getData($sql, $isFail);
        $totalNum           = $this->DbAuto->getDataInt('SELECT FOUND_ROWS() as num', 'num');
        if($isFail) return null;
        $AdOpenVOList       = [];
        if(CommArray::isForeach($adOpenRecordList))foreach($adOpenRecordList as $adOpenRecord){
            $adopIdItem     = intval($adOpenRecord['adop_id']);
            if($adopIdItem < 1) continue;
            $AdOpenVO       = $this->getAdOpenVO($adOpenRecord);
            if(!($AdOpenVO instanceof AdOpenVO)) continue;
            $AdOpenVOList[] = $AdOpenVO;
        }
        $AdOpenListVO       = new AdOpenListVO();
        $AdOpenListVO->setList($AdOpenVOList);
        $AdOpenListVO->setPage($Page->getPageVO($totalNum));
        return $AdOpenListVO;
    }

    /**
     * @param int $adopId
     * @return AdOpenVO|null
     * getAdOpenInfo
     */
    public function getAdOpenInfo(int $adopId):?AdOpenVO
    {
        $adOpenRecord   = $this->getAdOpenSrc($adopId);
        if(!isset($adOpenRecord['adop_id'])) return null;
        return $this->getAdOpenVO($adOpenRecord);
    }

    /**
     * @param array $adOpenRecord
     * @return AdOpenVO|null
     * getAdOpenVO
     */
    public function getAdOpenVO(array $adOpenRecord): ?AdOpenVO
    {
        $adopId         = intval($adOpenRecord['adop_id']??0);
        if($adopId < 1) return null;
        $AdOpenVO       = new AdOpenVO();
        $AdOpenVO->setAdopId($adopId);
        $AdOpenVO->setDdltId(intval($adOpenRecord['ddlt_id']));
        $AdOpenVO->setTitle($adOpenRecord['adop_title']);
        $AdOpenVO->setType(OpenAdMediaTypeEnum::parseValue($adOpenRecord['adop_type']));
        $AdOpenVO->setMediaOneUrl(StorageCtl::tag()->getStorageUrl(trim($adOpenRecord['adop_media_one']), true));
        $AdOpenVO->setMediaTwoUrl(StorageCtl::tag()->getStorageUrl(trim($adOpenRecord['adop_media_two']), true));
        $AdOpenVO->setMediaThrUrl(StorageCtl::tag()->getStorageUrl(trim($adOpenRecord['adop_media_thr']), true));
        $AdOpenVO->setBgColor($adOpenRecord['adop_bg_color']);
        $AdOpenVO->setShowMode(OpenAdShowModeEnum::parseValue($adOpenRecord['adop_show_mode']));
        $AdOpenVO->setShowNum(intval($adOpenRecord['adop_show_num']));
        $AdOpenVO->setUrl($adOpenRecord['adop_url']);
        $AdOpenVO->setShowTime(intval($adOpenRecord['adop_show_time']));
        $AdOpenVO->setSkipTime(intval($adOpenRecord['adop_skip_time']));
        $AdOpenVO->setDayTimeList($adOpenRecord['adop_day_time_list']);
        $AdOpenVO->setStatus(YesOrNoEnum::parseValue($adOpenRecord['adop_status']));
        $AdOpenVO->setSort(intval($adOpenRecord['adop_sort']));
        $AdOpenVO->setCreateTime(intval($adOpenRecord['adop_create_time']));
        $AdOpenVO->setUpdateTime(intval($adOpenRecord['adop_update_time']));
        return $AdOpenVO;
    }

    /**
     * @param array $setData
     * @return int
     * adOpenAdd
     */
    public function adOpenAdd(array $setData):int
    {
        if(count($setData) < 1) return 0;
        $currTime   = CommTime::getTimeStamp();
        $setString  = '';
        $admUserId  = AuthData::getLoginUserId();
        $dsacId     = DefStorageCategoryCtl::tag()->getIdByKey(DefStorageCategoryCtl::KEY_AD_IMAGE);
        foreach ($setData as $key => $val){
            switch ($key){
                case AdOpenAddDTO::_ddltId:
                    $setString  .= (strlen($setString) > 0 ? ', ' : '') . 'ddlt_id='.intval($val);
                    break;
                case AdOpenAddDTO::_title:
                    $setString  .= (strlen($setString) > 0 ? ', ' : '') . 'adop_title=\''.$this->DbAuto->getRealEscapeString($val) .'\'';
                    break;
                case AdOpenAddDTO::_type:
                    $setString  .= (strlen($setString) > 0 ? ', ' : '') . 'adop_type='.intval($val);
                    break;
                case AdOpenAddDTO::_mediaOneKey:
                    if($dsacId < 1) return 0;
                    $saveUrl    = StorageCtl::tag()->getStorageUrlRecord($admUserId, $dsacId, $val);
                    if(strlen($saveUrl) < 1) return 0;
                    $setString  .= (strlen($setString) > 0 ? ', ' : '') . 'adop_media_one=\''.$this->DbAuto->getRealEscapeString($saveUrl) .'\'';
                    break;
                case AdOpenAddDTO::_mediaTwoKey:
                    if($dsacId < 1) return 0;
                    $saveUrl    = StorageCtl::tag()->getStorageUrlRecord($admUserId, $dsacId, $val);
                    if(strlen($saveUrl) < 1) return 0;
                    $setString  .= (strlen($setString) > 0 ? ', ' : '') . 'adop_media_two=\''.$this->DbAuto->getRealEscapeString($saveUrl) .'\'';
                    break;
                case AdOpenAddDTO::_mediaThrKey:
                    if($dsacId < 1) return 0;
                    $saveUrl    = StorageCtl::tag()->getStorageUrlRecord($admUserId, $dsacId, $val);
                    if(strlen($saveUrl) < 1) return 0;
                    $setString  .= (strlen($setString) > 0 ? ', ' : '') . 'adop_media_thr=\''.$this->DbAuto->getRealEscapeString($saveUrl) .'\'';
                    break;
                case AdOpenAddDTO::_bgColor:
                    $setString  .= (strlen($setString) > 0 ? ', ' : '') . 'adop_bg_color=\''.$this->DbAuto->getRealEscapeString($val) .'\'';
                    break;
                case AdOpenAddDTO::_showMode:
                    $setString  .= (strlen($setString) > 0 ? ', ' : '') . 'adop_show_mode='.intval($val);
                    break;
                case AdOpenAddDTO::_showNum:
                    $setString  .= (strlen($setString) > 0 ? ', ' : '') . 'adop_show_num='.intval($val);
                    break;
                case AdOpenAddDTO::_url:
                    $setString  .= (strlen($setString) > 0 ? ', ' : '') . 'adop_url=\''.$this->DbAuto->getRealEscapeString($val) .'\'';
                    break;
                case AdOpenAddDTO::_showTime:
                    $setString  .= (strlen($setString) > 0 ? ', ' : '') . 'adop_show_time='.intval($val);
                    break;
                case AdOpenAddDTO::_skipTime:
                    $setString  .= (strlen($setString) > 0 ? ', ' : '') . 'adop_skip_time='.intval($val);
                    break;
                case AdOpenAddDTO::_dayTimeList:
                    $setString  .= (strlen($setString) > 0 ? ', ' : '') . 'adop_day_time_list=\''.$this->DbAuto->getRealEscapeString($val) .'\'';
                    break;
                case AdOpenAddDTO::_status:
                    $setString  .= (strlen($setString) > 0 ? ', ' : '') . 'adop_status='.intval($val);
                    break;
                case AdOpenAddDTO::_sort:
                    $setString  .= (strlen($setString) > 0 ? ', ' : '') . 'adop_sort='.intval($val);
                    break;
            }
        }
        if(strlen($setString) < 1) return 0;
        $isFail     = false;
        $sql        = 'INSERT INTO '.$this->DbAuto->getTableNameAll('ad_open').' SET '.$setString.', adop_create_time='.$currTime.', adop_update_time='.$currTime;
        $adopId     = $this->DbAuto->getDataId($sql, $isFail);
        if($isFail || $adopId < 1) return 0;
        return $adopId;
    }

    /**
     * @param int $adopId
     * @param array $setData
     * @return bool
     * adOpenEdit
     */
    public function adOpenEdit(int $adopId, array $setData):bool
    {
        if($adopId < 1 || count($setData) < 1) return false;
        $currTime       = CommTime::getTimeStamp();
        $admUserId      = AuthData::getLoginUserId();
        $setString      = '';
        $dsacId         = DefStorageCategoryCtl::tag()->getIdByKey(DefStorageCategoryCtl::KEY_AD_IMAGE);
        foreach ($setData as $key => $val){
            switch ($key){
                case AdOpenEditDTO::_ddltId:
                    $setString  .= (strlen($setString) > 0 ? ', ' : '') . 'ddlt_id='.intval($val);
                    break;
                case AdOpenEditDTO::_title:
                    $setString  .= (strlen($setString) > 0 ? ', ' : '') . 'adop_title=\''.$this->DbAuto->getRealEscapeString($val) .'\'';
                    break;
                case AdOpenEditDTO::_type:
                    $setString  .= (strlen($setString) > 0 ? ', ' : '') . 'adop_type='.intval($val);
                    break;
                case AdOpenEditDTO::_mediaOneKey:
                    if($dsacId < 1) return false;
                    $saveUrl    = StorageCtl::tag()->getStorageUrlRecord($admUserId, $dsacId, $val);
                    if(strlen($saveUrl) < 1) return false;
                    $setString  .= (strlen($setString) > 0 ? ', ' : '') . 'adop_media_one=\''.$this->DbAuto->getRealEscapeString($saveUrl) .'\'';
                    break;
                case AdOpenEditDTO::_mediaTwoKey:
                    if($dsacId < 1) return false;
                    $saveUrl    = StorageCtl::tag()->getStorageUrlRecord($admUserId, $dsacId, $val);
                    if(strlen($saveUrl) < 1) return false;
                    $setString  .= (strlen($setString) > 0 ? ', ' : '') . 'adop_media_two=\''.$this->DbAuto->getRealEscapeString($saveUrl) .'\'';
                    break;
                case AdOpenEditDTO::_mediaThrKey:
                    if($dsacId < 1) return false;
                    $saveUrl    = StorageCtl::tag()->getStorageUrlRecord($admUserId, $dsacId, $val);
                    if(strlen($saveUrl) < 1) return false;
                    $setString  .= (strlen($setString) > 0 ? ', ' : '') . 'adop_media_thr=\''.$this->DbAuto->getRealEscapeString($saveUrl) .'\'';
                    break;
                case AdOpenEditDTO::_bgColor:
                    $setString  .= (strlen($setString) > 0 ? ', ' : '') . 'adop_bg_color=\''.$this->DbAuto->getRealEscapeString($val) .'\'';
                    break;
                case AdOpenEditDTO::_showMode:
                    $setString  .= (strlen($setString) > 0 ? ', ' : '') . 'adop_show_mode='.intval($val);
                    break;
                case AdOpenEditDTO::_showNum:
                    $setString  .= (strlen($setString) > 0 ? ', ' : '') . 'adop_show_num='.intval($val);
                    break;
                case AdOpenEditDTO::_url:
                    $setString  .= (strlen($setString) > 0 ? ', ' : '') . 'adop_url=\''.$this->DbAuto->getRealEscapeString($val) .'\'';
                    break;
                case AdOpenEditDTO::_showTime:
                    $setString  .= (strlen($setString) > 0 ? ', ' : '') . 'adop_show_time='.intval($val);
                    break;
                case AdOpenEditDTO::_skipTime:
                    $setString  .= (strlen($setString) > 0 ? ', ' : '') . 'adop_skip_time='.intval($val);
                    break;
                case AdOpenEditDTO::_dayTimeList:
                    $setString  .= (strlen($setString) > 0 ? ', ' : '') . 'adop_day_time_list=\''.$this->DbAuto->getRealEscapeString($val) .'\'';
                    break;
                case AdOpenEditDTO::_status:
                    $setString  .= (strlen($setString) > 0 ? ', ' : '') . 'adop_status='.intval($val);
                    break;
                case AdOpenEditDTO::_sort:
                    $setString  .= (strlen($setString) > 0 ? ', ' : '') . 'adop_sort='.intval($val);
                    break;
            }
        }
        if(strlen($setString) < 1) return false;
        $isFail     = false;
        $sql        = 'UPDATE '.$this->DbAuto->getTableNameAll('ad_open').' SET '.$setString.', adop_update_time='.$currTime.' WHERE adop_id='.$adopId.' AND adop_delete=0';
        $this->DbAuto->getDataNum($sql, $isFail);
        if($isFail) return false;
        return true;
    }
}